import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '@/views/home/home.vue'
import NewsDetail from '@/views/home/news_detail'

import Login from '@/views/login.vue'
import Register from '../views/register.vue'

import Knowledge from '@/views/knowledge/knowledge'
import PageList from '../views/knowledge/pagelist'
import Page from '../views/knowledge/page'

import My from '@/views/my/my'
import Setting from '../views/my/setting'
import Password from '../views/my/password'
import Collect from '../views/my/mycollect';
import MyExams from '../views/my/myexams'
import ExamInfo from '../views/my/examinfo'
import Comments from '../views/my/mycomments';

import NotFound from '../views/notfound'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {
      auth: false,
    },
  },

  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      auth: false,
    },
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
    meta: {
      auth: false,
    }
  },
  {
    path: '/knowledge',
    name: 'knowledge',
    component: Knowledge,
    meta: {
      auth: false,
    }
  },
  {
    path: '/page/list',
    name: 'pagelist',
    component: PageList,
    meta: {
      auth: false,
    }
  },
  {
    path: '/page/:id',
    name: 'page',
    component: Page,
    meta: {
      auth: false,
    }
  },

  {
    path: '/my',
    name: 'my',
    component: My,
    meta: {
      auth: true,
    }
  },
  {
    path: '/show/myexams',
    name: 'myexams',
    component: MyExams,
    meta: {
      auth: true,
    }
  },
  // 动态路径参数 以冒号开头
  {
    path: '/show/examinfo/:id',
    name: 'examinfo',
    component: ExamInfo,
    meta: {
      auth: true,
    }
  },
  {
    path: '/show/mycollects',
    name: 'collect',
    component: Collect,
    meta: {
      auth: true,
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: NewsDetail,

    meta: {
      auth: false,
    }
  },
  {
    path: '/show/mycomments',
    name: 'comments',
    component: Comments,
    meta: {
      auth: true,
    }
  },
  {
    path: '/setting',
    name: 'setting',
    component: Setting,
    meta: {
      auth: true,
    }
  },
  {
    path: '/password',
    name: 'password',
    component: Password,
    meta: {
      auth: true,
    }
  },

  // 404页面，定义在最底部
  {
    // 匹配所有路由
    path: '*',
    name: 'notfound',
    component: NotFound,
    meta: {
      auth: false,
    }
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  linkActiveClass: 'tab-active',
  routes
})

router.beforeEach((to, from, next) => {

  // 判断用户是否登录
  if (to.path != '/login' && to.meta.auth && !localStorage.getItem('token')) {
    next('/login');
    return;
    // 已经登录的不能跳转到登录页面
  } else if (to.path == '/login' && localStorage.getItem('token')) {
    next(from);
    return;
  } else {
    next();
  }

})


export default router
